import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;

/**
 * @ClassName RSAKeys
 * @Description TODO
 * @Author 赵雷棚
 * @Date 2024/6/26 14:10
 **/
// 生成RSA密钥对
public class RSAKeys {
    public static void main(String[] args) throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048); // 设置密钥长度为2048位
        KeyPair keyPair = keyGen.generateKeyPair();

        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();

        System.out.println("Private Key:\n" + new String(Base64.getEncoder().encode(privateKey.getEncoded())));
        System.out.println("\nPublic Key:\n" + new String(Base64.getEncoder().encode(publicKey.getEncoded())));
    }
}
